package com.mf.morefun.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.mf.morefun.R;
import com.mf.morefun.bean.TestRankBean;
import com.mf.morefun.widget.TransformationMineUtils;

import org.jetbrains.annotations.Nullable;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.support.constraint.Constraints.TAG;

public class TestRankAdapter extends BaseAdapter {
    private Context mContext;
    private List<TestRankBean> testRankList;

    public TestRankAdapter(Context mContext, List<TestRankBean> testRankList) {
        this.mContext = mContext;
        this.testRankList = testRankList;
    }

    @Override
    public int getCount() {
        return testRankList.size();
    }

    @Override
    public Object getItem(int position) {
        return testRankList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (null == convertView) {
            view = LayoutInflater.from(mContext).inflate(R.layout.layout_testrankitem, null);
        } else {
            view = convertView;
        }
        if (null != testRankList && testRankList.size() > 0) {
            ViewHolder holder = new ViewHolder(view);
            holder.tvName.setText(testRankList.get(position).getUser().getName());
            holder.tvTime.setText(testRankList.get(position).getCostSecond() + "");
            if (position >= 0 && position <= 2) {
                holder.ivRank.setVisibility(View.VISIBLE);
                holder.tvRank.setVisibility(View.GONE);
                if (position == 0) {
                    holder.ivRank.setImageResource(R.mipmap.first);
                } else if (position == 1) {
                    holder.ivRank.setImageResource(R.mipmap.second);
                } else {
                    holder.ivRank.setImageResource(R.mipmap.three);
                }
            } else {
                holder.ivRank.setVisibility(View.GONE);
                holder.tvRank.setVisibility(View.VISIBLE);
                holder.tvRank.setText((position + 1) + "");
            }
            if ((position % 2) == 0){
                holder.llBg.setBackgroundColor(0xfff0f0f0);
            }else {
                holder.llBg.setBackgroundColor(0xffffffff);
            }
        }
        return view;
    }

    private class mRequestListener implements RequestListener {
        private ImageView ivPersonhead;

        public mRequestListener(ImageView ivPersonhead) {
            this.ivPersonhead = ivPersonhead;
        }

        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
            Log.d(TAG, "onException: " + e.toString() + "  model:" + model + " isFirstResource: " + isFirstResource);
            Message msg = new Message();
            msg.what = 100;
            msg.obj = ivPersonhead;
            mHandler.sendMessage(msg);
            return false;
        }

        @Override
        public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
            Log.e(TAG, "model:" + model + " isFirstResource: " + isFirstResource);
            return false;
        }
    }

    ;


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 100:
                    Glide.with(mContext).asBitmap().load(R.mipmap.mine_user_icon).into(new TransformationMineUtils((ImageView) msg.obj, mContext));
                    break;
            }
        }
    };

    static class ViewHolder {
        @BindView(R.id.iv_rank)
        ImageView ivRank;
        @BindView(R.id.tv_rank)
        TextView tvRank;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.ll_bg)
        LinearLayout llBg;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
